/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * BookInfoPanel.java
 *
 * Created on Mar 30, 2011, 8:10:36 PM
 */

package org.libarae.client;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.net.SocketTimeoutException;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import org.libarae.communication.ClientInterface;
import org.libarae.communication.common.Book;
import org.libarae.communication.common.Book.BookStatus;
import org.libarae.communication.request.BookInfoRequest;
import org.libarae.communication.response.BookInfoResponse;
import org.libarae.communication.response.Response;

/**
 *
 * @author Casey, Ben
 */
@SuppressWarnings("serial")
public class BookInfoPanel extends javax.swing.JPanel {

    private Book mBook;
    private MainScreen mMainScreen;

    private final int SCALED_IMAGE_WIDTH = 200;
    private final int SCALED_IMAGE_HEIGHT = 200;
    
    /** Creates new form BookInfoPanel */
    public BookInfoPanel()
    {
        initComponents();
    }

    public BookInfoPanel(MainScreen mMainScreen) {
        this.mMainScreen = mMainScreen;
        initComponents();
    }

    public void setBook(Book mBook)
    {
        this.mBook = mBook;

        lblTitle.setText(mBook.getTitle());
        lblAuthor.setText(mBook.getAuthor());
        lblISBN.setText(mBook.getISBN());
        lblPublisher.setText(mBook.getPublisher());
        lblPubDate.setText(mBook.getPubDate());
        txtSummary.setText(mBook.getSummary());
        if(mBook.getPicture() != null)
                lblPicture.setIcon(new ImageIcon(scaleImage(mBook.getPicture())));

        lblStatus.setText(mBook.getBookStatus().toString());

        if(mBook.getBookStatus() == BookStatus.CHECKED_OUT || mBook.getBookStatus() == BookStatus.PENDING)
            btnRequest.setEnabled(false);
        else
            btnRequest.setEnabled(true);

        this.revalidate();
    }

    private BufferedImage scaleImage(BufferedImage i)
    {
        //Scale the image down;
        int width;
        int height;

        if(i.getWidth() > i.getHeight())
        {
            width = SCALED_IMAGE_WIDTH;
            height = (int)(SCALED_IMAGE_HEIGHT * (((float)i.getHeight())/i.getWidth()));
        }
        else
        {
            height = SCALED_IMAGE_HEIGHT;
            width = (int)(SCALED_IMAGE_WIDTH * (((float)i.getWidth())/i.getHeight()));
        }


        BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = newImage.createGraphics();

        g.drawImage(i, 0, 0, width, height, this);
        g.dispose();

        return newImage;
    }

    public void refreshBook()
    {
        BookInfoRequest r = new BookInfoRequest();

        r.setBid(mBook.getBID());

        Response response = null;

        try {
            response = ClientInterface.sendRequest(r);
        }
        catch(SocketTimeoutException e)
        {
            JOptionPane.showMessageDialog(null, "Request timed out. Please try again.");
            return;
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        if(response == null)
        {
            System.err.println("Couldn't contact server.");
        }

        if(response.getStatus() == Response.STATUS_GOOD)
        {
            BookInfoResponse bir = (BookInfoResponse)response;

            if(bir.isMyBook())
            {
                btnEdit.setEnabled(true);
            }
            else
            {
                btnEdit.setEnabled(false);
            }
            this.mBook = bir.getBook();
            setBook(this.mBook);
        }

        //TODO: Handle failure
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblPicture = new javax.swing.JLabel();
        lbTitleLabel = new javax.swing.JLabel();
        btnRequest = new javax.swing.JButton();
        scrSummary = new javax.swing.JScrollPane();
        txtSummary = new javax.swing.JTextPane();
        lblAuthorLabel = new javax.swing.JLabel();
        lblISBNLabel = new javax.swing.JLabel();
        lblPublisherLabel = new javax.swing.JLabel();
        lblPubDateLabel = new javax.swing.JLabel();
        lblSummaryLabel = new javax.swing.JLabel();
        lblTitle = new javax.swing.JLabel();
        lblAuthor = new javax.swing.JLabel();
        lblISBN = new javax.swing.JLabel();
        lblPublisher = new javax.swing.JLabel();
        lblPubDate = new javax.swing.JLabel();
        lblStatusLabel = new javax.swing.JLabel();
        lblStatus = new javax.swing.JLabel();
        btnEdit = new javax.swing.JButton();

        lblPicture.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        lbTitleLabel.setText("Title:");

        btnRequest.setText("Request");
        btnRequest.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRequestActionPerformed(evt);
            }
        });

        txtSummary.setEditable(false);
        scrSummary.setViewportView(txtSummary);

        lblAuthorLabel.setText("Author:");

        lblISBNLabel.setText("ISBN:");

        lblPublisherLabel.setText("Publisher:");

        lblPubDateLabel.setText("Pub. Date:");

        lblSummaryLabel.setText("Summary:");

        lblTitle.setText(" ");

        lblAuthor.setText(" ");

        lblISBN.setText(" ");

        lblPublisher.setText(" ");

        lblPubDate.setText(" ");

        lblStatusLabel.setText("Status:");

        lblStatus.setText(" ");

        btnEdit.setText("Edit");
        btnEdit.setEnabled(false);
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(lblPicture, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnRequest)
                                .addGap(6, 6, 6)
                                .addComponent(btnEdit))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lbTitleLabel)
                                    .addComponent(lblAuthorLabel)
                                    .addComponent(lblISBNLabel)
                                    .addComponent(lblPublisherLabel)
                                    .addComponent(lblPubDateLabel)
                                    .addComponent(lblStatusLabel))
                                .addGap(23, 23, 23)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lblStatus)
                                    .addComponent(lblPubDate)
                                    .addComponent(lblPublisher)
                                    .addComponent(lblISBN)
                                    .addComponent(lblAuthor)
                                    .addComponent(lblTitle)))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblSummaryLabel)
                            .addComponent(scrSummary, javax.swing.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(lblPicture, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(41, 41, 41)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lbTitleLabel)
                            .addComponent(lblTitle))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblAuthorLabel)
                            .addComponent(lblAuthor))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblISBNLabel)
                            .addComponent(lblISBN))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblPublisherLabel)
                            .addComponent(lblPublisher))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblPubDateLabel)
                            .addComponent(lblPubDate))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblStatusLabel)
                            .addComponent(lblStatus))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnRequest)
                            .addComponent(btnEdit))))
                .addGap(8, 8, 8)
                .addComponent(lblSummaryLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(scrSummary, javax.swing.GroupLayout.DEFAULT_SIZE, 61, Short.MAX_VALUE)
                .addGap(45, 45, 45))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnRequestActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRequestActionPerformed
        BookRequestDialog brd = new BookRequestDialog(mBook, mMainScreen);
        brd.setVisible(true);
    }//GEN-LAST:event_btnRequestActionPerformed

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        AddEditBookDialog ebd = new AddEditBookDialog(AddEditBookDialog.EDIT_BOOK, mMainScreen);

        ebd.displayBook(mBook);
        ebd.setVisible(true);
    }//GEN-LAST:event_btnEditActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnRequest;
    private javax.swing.JLabel lbTitleLabel;
    private javax.swing.JLabel lblAuthor;
    private javax.swing.JLabel lblAuthorLabel;
    private javax.swing.JLabel lblISBN;
    private javax.swing.JLabel lblISBNLabel;
    private javax.swing.JLabel lblPicture;
    private javax.swing.JLabel lblPubDate;
    private javax.swing.JLabel lblPubDateLabel;
    private javax.swing.JLabel lblPublisher;
    private javax.swing.JLabel lblPublisherLabel;
    private javax.swing.JLabel lblStatus;
    private javax.swing.JLabel lblStatusLabel;
    private javax.swing.JLabel lblSummaryLabel;
    private javax.swing.JLabel lblTitle;
    private javax.swing.JScrollPane scrSummary;
    private javax.swing.JTextPane txtSummary;
    // End of variables declaration//GEN-END:variables

}
